#ifndef SPHERE_H
#define SPHERE_H

#include"hittable.h"



class sphere :public hittable {
public:
	sphere(const point3& _center,double _radius, shared_ptr<material> _mat): center(_center),radius(std::fmax(0,_radius)),mat(_mat) {
	//initialize mat

	}

	bool hit(const ray& _ray, interval ray_t, hit_record& rec) const override {
		//describe hit point on sphere
		vec3 oc = center - _ray.origin();
		auto a = _ray.direction().length_squared();
		auto h = dot(_ray.direction(), oc);
		auto c = oc.length_squared() - radius * radius;
		auto discri = h * h - a * c;

		if (discri < 0) return false;

		auto sqrtd = std::sqrt(discri);

		auto root = (h - sqrtd) / a;
		if (!ray_t.surrounds(root)) {
			root = (h + sqrtd) / a;
			if (!ray_t.surrounds(root)) {
				return false;
			}
		}

		rec.t = root;
		rec.pt = _ray.at(rec.t);
		vec3 outward_normal = (rec.pt - center) / radius;
		rec.set_face_normal(_ray, outward_normal);
		rec.mat = mat;
		
		//rec.set_face_normal(_ray, out_norm);

		return true;
		}




private:
	point3 center;
	double radius;
	shared_ptr<material> mat;
};



#endif // !SPHERE_H
